<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>mscanfmfscanfmsscanf</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 07/04/2006</div>
    <p>
      <b>mscanf</b> -  interface to the C scanf function</p>
    <p>
      <b>mfscanf</b> -  interface to the C fscanf function</p>
    <p>
      <b>msscanf</b> -  interface to the C sscanf function</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[n,v_1,...v_n]=mfscanf([niter,]fd,format)  </tt>
      </dd>
      <dd>
        <tt>L=mfscanf([niter,] fd,format)  </tt>
      </dd>
      <dd>
        <tt></tt>
      </dd>
      <dd>
        <tt>[n,v_1,...v_n]=mscanf([niter,] format)  </tt>
      </dd>
      <dd>
        <tt>L=mscanf([niter,]format)  </tt>
      </dd>
      <dd>
        <tt></tt>
      </dd>
      <dd>
        <tt>[n,v_1,...v_m]=msscanf([niter,]str,format)  </tt>
      </dd>
      <dd>
        <tt>L=msscanf([niter,] str,format)  </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>format</b>
        </tt>: a Scilab string describing the format to use to write the
	    remaining operands.  The format operand follows, as close as
	    possible, the C printf format operand syntax.</li>
      <li>
        <tt>
          <b>fd</b>
        </tt>:The fd parameter returned by the function <tt>
          <b>mopen</b>
        </tt> is
	    used as a file descriptor (it's a positive integer). The value -1
	    refers to the  last opened file.</li>
      <li>
        <tt>
          <b>str</b>
        </tt>: a Scilab string or string vector.</li>
      <li>
        <tt>
          <b>niter</b>
        </tt>: an integer, the number of times the format as to
	    be used.</li>
      <li>
        <tt>
          <b>n</b>
        </tt>: an integer, the number of data read or -1 if EOL has been
	    encountered before any datum has been read.</li>
      <li>
        <tt>
          <b>v_i</b>
        </tt>: Each function reads characters, interprets them according to a
	    format, and stores the results in its output arguments.  If more
	    than $n$ output arguments are provided, the last ones
	    <tt>
          <b>v_n+1,...v_m</b>
        </tt> are set to empty matrices.</li>
      <li>
        <tt>
          <b>L</b>
        </tt>: if all data are homogeneous they are stored in a unique vector 
	    which is returned, otherwise subsequences of same data type are 
	    stored in matrices and an <tt>
          <b>mlist</b>
        </tt> (with type
	    <tt>
          <b>cblock</b>
        </tt>) containing all the built matrices is returned.</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
      The <tt>
        <b>mfscanf</b>
      </tt> function reads characters from the stream <tt>
        <b>fd</b>
      </tt>.</p>
    <p>
      The <tt>
        <b>mscanf</b>
      </tt> function reads characters from Scilab window.</p>
    <p>
      The <tt>
        <b>msscanf</b>
      </tt> function reads characters from the Scilab string <tt>
        <b>str</b>
      </tt>.</p>
    <p>
      The <tt>
        <b>niter</b>
      </tt> optional argument specifies how many time
      the format has to used. One iteration produces one line in the
      output matrix. If <tt>
        <b>niter==-1</b>
      </tt> the function iterates up
      to the end of file. The <tt>
        <b>niter</b>
      </tt> default value is 1.</p>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>
    
    //--    Simple use                                        --
    //----------------------------------------------------------
    s='1 1.3'  //a string
    [n,a,b]=msscanf(s,"%i %e")
    L=msscanf(s,"%i %e")

    //--    Formats samples                                   --
    //----------------------------------------------------------

    msscanf(" 12\n",'%c%c%c%c') //scan characters

    msscanf('0xabc','%x') //scan with hexadecimal format

    msscanf('012345abczoo','%[0-9abc]%s')  //[] notation

    // reading float and double
    msscanf('4345.988','%g')-4345.988 // scan as float
    msscanf('4345.988','%lg')-4345.988 // scan as double


    //--    scanning multi-line data files                    --
    //----------------------------------------------------------
    //create a file with data
    u=mopen(TMPDIR+'/foo','w');
    t=(0:0.1:%pi)';mfprintf(u,"%6.3f %6.3f\n",t,sin(t))
    mclose(u);

    u=mopen(TMPDIR+'/foo','r'); // open the file for reading
    //read the file line by line
    [n,a,b]=mfscanf(u,'%e %e') //first line using mutiple LHS syntax
    l=mfscanf(u,'%e %e')       //second one using single LHS syntax
    //use niter to read 5 more lines
    l=mfscanf(5,u,'%e %e')  

    //use niter=-1 to read up to the end of file
    l=mfscanf(-1,u,'%e %e')  

    mclose(u); //close the file



    //--    scanning multi-line  strings vectors              --
    //----------------------------------------------------------

    //use niter to scan a string vector
    [n,Names,Ages]=msscanf(-1,["Alain 19";"Pierre 15";"Tom 12"],'%s %d')
    D=msscanf(-1,["Alain 19";"Pierre 15";"Tom 12"],'%s %d')
    typeof(D)
    Names=D(:,1) //strings
    Age=D(:,2)   //numerical values


    
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="mclose.htm">
        <tt>
          <b>mclose</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="meof.htm">
        <tt>
          <b>meof</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mprintf.htm">
        <tt>
          <b>mfprintf</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="fprintfMat.htm">
        <tt>
          <b>fprintfMat</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mfscanf.htm">
        <tt>
          <b>mfscanf</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="fscanfMat.htm">
        <tt>
          <b>fscanfMat</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mget.htm">
        <tt>
          <b>mget</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mgetstr.htm">
        <tt>
          <b>mgetstr</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mopen.htm">
        <tt>
          <b>mopen</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mprintf.htm">
        <tt>
          <b>mprintf</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mput.htm">
        <tt>
          <b>mput</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mputstr.htm">
        <tt>
          <b>mputstr</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mfscanf.htm">
        <tt>
          <b>mscanf</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mseek.htm">
        <tt>
          <b>mseek</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mtell.htm">
        <tt>
          <b>mtell</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="mdelete.htm">
        <tt>
          <b>mdelete</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
  </body>
</html>
